use std::{
    fs::OpenOptions,
    io::{prelude::*, BufReader},
    path::PathBuf,
};

use anyhow::{anyhow, Result};

pub fn file_exist(path: &PathBuf) -> Result<()> {
    if !path.exists() {
        return Err(anyhow!("file_exist: file not exist"));
    } else if !path.is_file() {
        return Err(anyhow!("file_exist: is not file"));
    }
    Ok(())
}

pub fn handle_lines(path: &PathBuf, mut view_func: impl FnMut(&String)) -> Result<()> {
    file_exist(&path)?;
    let file = OpenOptions::new().read(true).open(&path)?;

    let reader = BufReader::new(file);
    for line in reader.lines() {
        view_func(&line?);
    }

    Ok(())
}
